<template>
  <a-card :bordered="false">
      <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="仓库名称" style="margin-left:8px">
              <a-select
                v-model="queryParam.storehouseId"
                placeholder="请选择仓库"
                :showSearch="true"
                :allowClear="true"
                @change="storehouseChange"
                :filterOption="filterOption"
                :getPopupContainer="triggerNode => {return triggerNode.parentNode}"
              >
                <a-select-option v-for="item in foldWarehouseList" :key="item.id" :value="item.id">{{ item.categoryName }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料大类">
              <a-select
                v-model="queryParam.provisionBroad"
                placeholder="请选择物料大类"
                :showSearch="true"
                :allowClear="true"
                :filterOption="filterOption"
                @change="handleChange"
                :getPopupContainer="(triggerNode)=>{ return triggerNode.parentNode}"
              >
                <a-select-option v-for="item in provisionBroadList" :key="item.id" :value="item.id">{{item.categoryName}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料小类">
              <a-select
                v-model="queryParam.provisionSmall"
                placeholder="请选择物料小类"
                :showSearch="true"
                :allowClear="true"
                :filterOption="filterOption"
                @change="handleSmallChange"
                :getPopupContainer="triggerNode => {return triggerNode.parentNode}"
              >
                <a-select-option v-for="item in provisionSmallList" :key="item.id" :value="item.id">{{item.categoryName}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料名称">
              <select-material-input v-model="queryParam.materialName" :materialFlag="true" :materialSmall="queryParam.provisionSmall" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="onClearSelected();searchQuery();" icon="search">查询</a-button>
              <a-button type="primary" @click="provisionSmallList=[],searchReset()" icon="reload" style="margin-left: 8px">重置</a-button>
              <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a> -->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="handleExportXls('库存变动记录表')" :disabled="isDisabledAuth('erpPurchaseSupplierStanding:export')">导出</a-button>
      <!-- 高级查询区域 -->
      <!--<j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-popover title="自定义列" trigger="click" placement="leftBottom">
        <template slot="content">
          <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
            <a-row>
              <template v-for="(item,index) in defColumns">
                <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                  <a-col :span="12" :key="index"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                </template>
              </template>
            </a-row>
          </a-checkbox-group>
        </template>
        <a style="float: right;"><a-icon type="setting" :style='iconStyle'/></a>
      </a-popover>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x: '180%'}"
        :components="draggableComponent"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">
        <template slot="provision" slot-scope="text,record">
          <span>{{record.provisionBroad_dictText && record.provisionSmall_dictText? record.provisionBroad_dictText + '-' + record.provisionSmall_dictText: '' }}</span>
        </template>
        <template slot="relatedBill" slot-scope="text,record">
          <a @click="relatedBillDetail(record)">{{ text }}</a>
        </template>
      </a-table>
    </div>
    <erp-purchase-cannibalize-modal ref="cannibalize"></erp-purchase-cannibalize-modal>
    <ErpPurchaseWarehouseWarrantModal ref="warehouse"/>
    <ErpPurchaseWarehouseCheckBillModal ref="warehousecheck"/>
    <ErpPurchaseRecipientsModal ref="recipients"/>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import {DisabledAuthFilterMixin} from '@/mixins/DisabledAuthFilterMixin'
  import {getAction} from '@/api/manage'
  import { mapGetters } from 'vuex'
  import ErpPurchaseCannibalizeModal from '@/views/erp/purchase/recipients/modules/ErpPurchaseCannibalizeModal.vue'
  import ErpPurchaseRecipientsModal from '@/views/erp/purchase/recipients/modules/ErpPurchaseRecipientsModal.vue'
  import ErpPurchaseWarehouseWarrantModal from './modules/ErpPurchaseWarehouseWarrantModal.vue'
  import ErpPurchaseWarehouseCheckBillModal from './modules/ErpPurchaseWarehouseCheckBillModal.vue'
  import SelectMaterialInput from '@/views/scy/components/SelectMaterialInput'

  export default {
    name: 'ErpPurchaseInventoryChangesList',
    mixins:[JeecgListMixin, mixinDevice, DisabledAuthFilterMixin],
    components: {
      ErpPurchaseCannibalizeModal,
      ErpPurchaseWarehouseWarrantModal,
      ErpPurchaseWarehouseCheckBillModal,
      ErpPurchaseRecipientsModal,
      SelectMaterialInput
    },
    data () {
      return {
        description: '库存变动记录页面',
        //表头
        columns:[],
        //列设置
        settingColumns:[],
        // 列定义
        defColumns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'仓库名称',
            align:"center",
            dataIndex: 'storehouseId_dictText',
            width: 300,
            ellipsis: true,
          },
          {
            title:'物料类型',
            align:"center",
            width: 200,
            dataIndex: 'provisionBroad',
            ellipsis: true,
            customRender:function (t,record,index) {
              return record.provisionBroad_dictText && record.provisionSmall_dictText? record.provisionBroad_dictText + '-' + record.provisionSmall_dictText: ''
            }
          },
          {
            title:'物料编号',
            align:"center",
            width: 200,
            dataIndex: 'materialBillId',
            ellipsis: true,
          },
          {
            title:'物料名称',
            align:"center",
            width: 200,
            dataIndex: 'materialName_dictText',
            ellipsis: true,
          },
          {
            title:'品牌',
            align:"center",
            width: 200,
            dataIndex: 'brand',
            ellipsis: true,
          },
          {
            title:'规格型号',
            align:"center",
            width: 200,
            dataIndex: 'specification',
            ellipsis: true,
          },
          {
            title:'单位',
            align:"center",
            width: 200,
            dataIndex: 'unit_dictText',
            ellipsis: true,
          },
          {
            title:'数量变化时间',
            align:"center",
            width: 200,
            dataIndex: 'changeTime',
            ellipsis: true,
          },
          {
            title:'变化原因',
            align:"center",
            width: 200,
            dataIndex: 'changeFrom_dictText',
            ellipsis: true,
          },
          {
            title:'关联单据',
            align:"center",
            width: 200,
            dataIndex: 'relatedBill',
            ellipsis: true,
            scopedSlots: {customRender: 'relatedBill'},
          },
          {
            title:'变化前数量',
            align:"center",
            width: 200,
            dataIndex: 'amountBeforeChange',
            ellipsis: true,
          },
          {
            title:'变化后数量',
            align:"center",
            width: 200,
            dataIndex: 'amountAfterChange',
            ellipsis: true,
          },
        ],
        url: {
          list: "/api/erp/change/erpStorehouseChangeInfo/list",
          exportXlsUrl: "/api/erp/change/erpStorehouseChangeInfo/exportXls",
          queryProvisionBroadList: "/api/erp/purchase/material/erpMaterialCategory/queryAllParent",
          queryProvisionSmallList: "/api/erp/purchase/material/erpMaterialCategory/queryChildrenByParentId",
          queryByStorehouseManager: '/api/erp/purchase/storehouse/erpStorehouseBase/queryByStorehouseManager'
        },
        dictOptions:{},
        superFieldList:[],
        provisionBroadList: [],
        provisionSmallList: [],
        foldWarehouseList: []
      }
    },
    created() {
      this.getSuperFieldList();
      this.findProvisionBroadList()
      this.findFoldApplicant()//根据单位查询仓库
    },
    methods: {
      ...mapGetters(['userInfo']),
      relatedBillDetail(record){
        let url = ''
        let key = ''
        let title = ''
        switch (record.relatedBill.slice(0,2)) {
          case 'WZ':

            title = '物资调拨详情'
            url = '/api/erp/purchase/recipients/erpPurchaseCannibalize/queryById'
            key = 'cannibalize'

            break;
          case 'WP':

            title = '物品领用详情'
            url = '/api/erp/purchase/recipients/erpPurchaseRecipients/queryById'
            key = 'recipients'
            
            break;
          case 'RK':

            title = '入库单详情'
            url = '/api/erp/purchase/storehouse/erpPurchaseWarehouseWarrant/queryById'
            key = 'warehouse'

            break;
          case 'PD':

            title = '盘点详情'
            url = '/api/erp/purchase/storehouse/erpPurchaseWarehouseCheckBill/queryById'
            key = 'warehousecheck'

            break;
        }
        getAction(url,{ id: record.relatedId }).then(res => {
          if(res.success){
            this.$refs[key].edit(res.result)
            this.$refs[key].title = title
            this.$refs[key].disableSubmit = true
          } else {
            this.$message.warning(res.message)
          }
        })
        
      },
      storehouseChange(data) {
        if (!data) {
          this.$set(this.queryParam, 'storehouseId', undefined)
        }
      },
      findFoldApplicant() {
        const that = this
        getAction(this.url.queryByStorehouseManager, { storehouseManager: this.userInfo().empId }).then((res) => {
          if (res.success) {
            that.foldWarehouseList = []
            res.result.forEach((res) => {
              that.foldWarehouseList.push({ 'id': res.id, 'categoryName': res.storehouseName })
            })
          } else {
            that.$message.warning(res.message)
          }
        })
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      findProvisionBroadList() {
        const that = this
        that.provisionBroadList = []
        getAction(this.url.queryProvisionBroadList).then(res => {
          if (res.success) {
            res.result.forEach((res) => {
              that.provisionBroadList.push({ 'id':res.id, 'categoryName':res.categoryName })
            })
          } else {
            that.$message.warning(res.message)
          }
        })
      },
      findProvisionSmallList(value) {
        const that = this
        const parentId = value
        getAction(this.url.queryProvisionSmallList, { parentId }).then(res => {
          if (res.success) {
            res.result.forEach((res) => {
              that.provisionSmallList.push({ 'id':res.id, 'categoryName':res.categoryName })
            })
          } else {
            that.$message.warning(res.message)
          }
        })
      },
      handleChange(value) {
        this.provisionSmallList = []
        this.$set(this.queryParam, 'provisionSmall', undefined);
        this.$set(this.queryParam, 'materialName', undefined);
        if (!!value) {
          this.findProvisionSmallList(value)
        }
      },
      handleSmallChange(){
        this.$set(this.queryParam, 'materialName', undefined);
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'enterpriseName',text:'企业名称',dictCode:''})
        fieldList.push({type:'string',value:'enterpriseCreditCode',text:'企业信用代码',dictCode:''})
        fieldList.push({type:'string',value:'enterpriseType',text:'企业类型',dictCode:''})
        fieldList.push({type:'string',value:'legalRepresentative',text:'法定代表人'})
        fieldList.push({type:'string',value:'enterpriseAddress',text:'企业地址',dictCode:''})
        fieldList.push({type:'date',value:'registerDate',text:'企业注册时间',dictCode:''})
        fieldList.push({type:'string',value:'registerCapital',text:'注册资本',dictCode:''})
        fieldList.push({type:'string',value:'bankName',text:'开户行',dictCode:''})
        fieldList.push({type:'string',value:'bankAccountNo',text:'银行账号',dictCode:''})
        fieldList.push({type:'string',value:'companyWeb',text:'公司网址',dictCode:''})
        fieldList.push({type:'string',value:'provisionBroad',text:'物料大类',dictCode:''})
        fieldList.push({type:'string',value:'provisionSmall',text:'物料小类',dictCode:''})
        fieldList.push({type:'string',value:'mainBussiness',text:'主营业务',dictCode:''})
        fieldList.push({type:'string',value:'contactInformation',text:'联系信息',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
